package com.example.zzl.view.fragment.UserFragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzl.R;
import com.example.zzl.app.MyApplication;
import com.example.zzl.view.BaseFragment;
import com.example.zzl.view.activity.UserActivity.LoginActivity;

import java.util.ArrayList;
import java.util.List;

public class UserFragment extends BaseFragment {
    private List<Integer> listl;
    private List<String> list;
    private List<Integer> listfood1;
    private List<String> listfood;

    private ListView lv;
    private UserAdapter adapter;

    private TextView tv,tv_nick;

    private View footview;
    private Mylist footlv;
    private FootAdapter footAdapter;
    private Button btn;


    private View headview;


    public static UserFragment newInstance() {

        Bundle args = new Bundle();

        UserFragment fragment = new UserFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_user, null);
        lv = (ListView) view.findViewById(R.id.lv);

        footview = View.inflate(getContext(), R.layout.foot_view, null);
        footlv = (Mylist) footview.findViewById(R.id.lv);

        headview = View.inflate(getContext(), R.layout.head, null);
        tv = (TextView) headview.findViewById(R.id.tv);
        tv_nick = (TextView) view.findViewById(R.id.tv_nickname);
        btn = (Button) view.findViewById(R.id.btn);
       //footlv.addFooterView(btn);

        init();
        return view;

    }

    private void init() {

        listl = new ArrayList<>();
        list = new ArrayList<>();
        listfood = new ArrayList<>();
        listfood1 = new ArrayList<>();
        listl.add(R.drawable.usercenter_icon_dingdan);
        listl.add(R.drawable.usercenter_icon_shoucang);
        listl.add(R.drawable.usercenter_icon_youhuiquan);
        listl.add(R.drawable.usercenter_icon_pinglun);
        listl.add(R.drawable.usercenter_icon_xiaoxi);
        listl.add(R.drawable.usercenter_icon_dizhiguanli);
        list.add("订单");
        list.add("收藏");
        list.add("优惠券");
        list.add("评论");
        list.add("消息");
        list.add("地址管理");
        adapter = new UserAdapter(getContext(), list, listl);

        lv.setAdapter(adapter);

        listfood.add("帮助说明");
        listfood.add("设置");
        listfood.add("推荐给朋友");
        listfood.add("给比亚比好评");
        listfood.add("关于");
        listfood1.add(R.drawable.usercenter_icon_bangzhu);
        listfood1.add(R.drawable.usercenter_icon_setting);
        listfood1.add(R.drawable.usercenter_icon_fenxiang);
        listfood1.add(R.drawable.usercenter_icon_haoping);
        listfood1.add(R.drawable.usercenter_icon_guanyu);
        footAdapter = new FootAdapter(getContext(), listfood, listfood1);

        lv.addFooterView(footview);
        lv.addHeaderView(headview);
        footlv.setAdapter(footAdapter);


            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(tv.getText().toString().equals("登录")) {
                        startActivityForResult(new Intent(getActivity(), LoginActivity.class), 789);
                    }else{
                        Toast.makeText(getActivity(), "签到", Toast.LENGTH_SHORT).show();
                    }
                }
            });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(MyApplication.nichName != null){
                    btn.setVisibility(View.GONE);
                    MyApplication.nichName = null;
                    tv_nick.setText("我");
                    tv.setText("登录");
                }
            }
        });


//
//        tv.setText("签到");
//        listfood.add("退出帐号");
//        footAdapter.notifyDataSetChanged();
//        footlv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                switch (v.getId()){
////
////                }
//
//            }
        //      });

//footlv.setOnClickListener(this);
        footlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent;
                if (position == 0) {
                    intent = new Intent(getContext(), WebActivity.class);
                    startActivity(intent);
                } else if (position == 4) {
                    intent = new Intent(getContext(), GuanyuActivity.class);
                    startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.activity_oper, 0);
                    //getActivity().overridePendingTransition
                }

            }

//    private void overridePendingTransition(int slide_in_left, int slide_out_right) {
//        return;
//    }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 789 && resultCode == 900){
            tv.setText("签到");
            tv_nick.setText(MyApplication.nichName);
            btn.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            if(MyApplication.nichName!=null){
                tv.setText("签到");
                tv_nick.setText(MyApplication.nichName);
                btn.setVisibility(View.VISIBLE);
            }else{
                btn.setVisibility(View.GONE);
                MyApplication.nichName = null;
                tv_nick.setText("我");
                tv.setText("登录");
            }
        }
    }
}
